package com.example.jmeterTest.controller;


import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@RestController
@RequestMapping("/memory")
public class OutofMemoryController {
    Log log = LogFactory.getLog(OutofMemoryController.class);

    // 创建线程池
     ExecutorService executorService = Executors.newFixedThreadPool(10);
    // 用于存储内存占用对象的列表
    private static final List<Object> memoryHolder = new ArrayList<>();

    /**
     * 模拟大对象
     */
    @RequestMapping(value = "/out", method = RequestMethod.GET)
    public String exec() {
        executorService.submit(() -> {
            try {
                // 创建一个大对象
//                    byte[] largeObject = new byte[10 * 1024 * 1024]; // 10MB
                byte[] largeObject = new byte[10 * 1024]; // 10MB

                // 将其加入内存占用列表中，防止被gc
                synchronized (memoryHolder) {
                    memoryHolder.add(largeObject);
                }

                log.trace("模拟填充大对象");

                // 模拟一些工作
                Thread.sleep(1000);

            } catch (OutOfMemoryError e) {
                System.err.println("Out of memory! Halting further allocation.");
                // 如果出现OOM，暂停分配以防止程序崩溃
                executorService.shutdown();

            } catch (InterruptedException e) {
                // 捕获并处理睡眠中断
                Thread.currentThread().interrupt();
                System.err.println("Task interrupted");
            }
        });
        return "ok";
    }
}
